import { Context, Schema, h } from 'koishi'
import {} from 'koishi-plugin-adapter-onebot';

import { getRandomIntInclusive } from './utils'

export const name = 'silence-me'

export interface Config {
  maxSilenceSecond: number,
  minSilenceSecond: number,
}

export const Config: Schema<Config> = Schema.intersect([
  Schema.object({
    maxSilenceSecond: Schema.number().min(60),
    minSilenceSecond: Schema.number().min(600),
  })
])

export function apply(ctx: Context, config: Config) {
  // write your plugin here
  ctx.command("mute_me")
    .alias("禁言我")
    .action( async ({session, options}) => {

      if ( !session.onebot ){
        let errmsg = "[error]当前会话不支持onebot协议。"
        await session.send(errmsg);
        ctx.logger.info(errmsg);
      }

      await session.send(
        h.quote(session.messageId) + "好啊，没问题啊"
      )

      const mute_dutation = getRandomIntInclusive(config.maxSilenceSecond, config.minSilenceSecond) || 60;
      await session.onebot._request('set_group_ban', {
        user_id: parseInt(session.userId),
        group_id: parseInt(session.guildId),
        duration: mute_dutation
      })

    })


}
